package cn.edu.hznu.ocw.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.edu.hznu.ocw.domain.SubType;
import cn.edu.hznu.ocw.jdbc.DBUtils;

public class SubTypeDAO {
	public List<SubType> getSubTypes() {
		try {
			String sql = "SELECT * FROM subtype ORDER BY sname ASC";
			Connection conn = DBUtils.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			List<SubType> list = new ArrayList<SubType>();
			while (rs.next()) {
				SubType st = new SubType();
				st.setId(rs.getInt("id"));
				st.setSname(rs.getString("sname"));
				st.setSdesc(rs.getString("sdesc"));
				st.setSftype(rs.getInt("sftype"));
				list.add(st);
			}
			DBUtils.close(rs, stmt, conn);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Set<SubType> getByFtype(int sftype){
		try {
			String sql = "SELECT * FROM subtype WHERE sftype=" + sftype + " ORDER BY sname ASC";
			Connection conn = DBUtils.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			Set<SubType> set = new HashSet<SubType>();
			while (rs.next()) {
				SubType st = new SubType();
				st.setId(rs.getInt("id"));
				st.setSname(rs.getString("sname"));
				st.setSdesc(rs.getString("sdesc"));
				st.setSftype(rs.getInt("sftype"));
				set.add(st);
			}
			DBUtils.close(rs, stmt, conn);
			return set;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
